# 设置最低版本号
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
# 设置项目名称
project(rk3588-demo VERSION 0.0.1 LANGUAGES CXX)

# 输出系统信息
message(STATUS "System: ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}")

# 设置编译器
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置库架构
set(LIB_ARCH "aarch64")
set(DEVICE_NAME "RK3588")

#  rknn_api 文件夹路径
set(RKNN_API_PATH ${CMAKE_CURRENT_SOURCE_DIR}/librknn_api)
#  rknn_api include 路径
set(RKNN_API_INCLUDE_PATH ${RKNN_API_PATH}/include)
#  rknn_api lib 路径
set(RKNN_API_LIB_PATH ${RKNN_API_PATH}/${LIB_ARCH}/librknnrt.so)

# 查找库的工具
find_package(PkgConfig REQUIRED)
# 寻找本地的opencv
find_package(OpenCV REQUIRED)
# 查找Threads（用于std::thread和互斥锁）
find_package(Threads REQUIRED)
# 查找wiringPi库
find_library(WIRINGPI_LIBRARY wiringPi)
# 查找PortAudio库--用于音频实时播放
pkg_check_modules(PORTAUDIO REQUIRED IMPORTED_TARGET portaudio-2.0)
# 查找FFTW3单精度库
pkg_check_modules(FFTW3 REQUIRED IMPORTED_TARGET fftw3f)
# 输出OpenCV信息
message(STATUS "include path: ${OpenCV_INCLUDE_DIRS}")

# 查找依赖库 读音频和进行fft的库
find_library(KISSFFT_LIB NAMES kissfft-float)  # 查找kissfft库
# 验证库是否找到
if(NOT KISSFFT_LIB)
    message(FATAL_ERROR "kissfft library not found")
endif()



# 用来搜索头文件的目录
include_directories(
    ${OpenCV_INCLUDE_DIRS}
    ${RKNN_API_INCLUDE_PATH}
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${RGA_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/include  # 你的头文件目录
    ${KISSFFT_INCLUDE_DIRS}              # 如果kissfft需要特殊包含路径
    ${CMAKE_CURRENT_SOURCE_DIR}/src/utool_AudioRT
)


# 构建DEEPFIR的预处理库
# deepfir_lib
add_library(deepfir_process SHARED src/process/preprocess_deepfir.cpp)
# 链接库
target_link_libraries(deepfir_process
    sndfile      # libsndfile的CMake目标
    ${OpenCV_LIBS}
    ${KISSFFT_LIB}        # kissfft库
    m                     # 数学库
)

# 构建自定义封装API库
add_library(rknn_engine SHARED src/engine/rknn_engine.cpp)
# 链接库
target_link_libraries(rknn_engine
    ${RKNN_API_LIB_PATH}
)

# deepfir_lib
add_library(deepfir_lib SHARED src/task/deepfir.cpp)
# 链接库
target_link_libraries(deepfir_lib
    rknn_engine
    deepfir_process
)

# 测试deepfir
add_executable(deepfir src/DeepFIR_test.cpp)
# 链接库
target_link_libraries(deepfir
        deepfir_lib
)


# 实时音频输入输出工程
add_library(Preprocess_CMP_RT_lib SHARED src/AscDeepFileRT/Preprocess_CMP_RT.cpp) # 添加动态库
add_library(DeepCMP_RT_lib SHARED src/AscDeepFileRT/DeepCMP_RT.cpp) # 添加动态库
target_link_libraries(DeepCMP_RT_lib
    Preprocess_CMP_RT_lib
    rknn_engine
    sndfile      # libsndfile的CMake目标
    ${KISSFFT_LIB}        # kissfft库
    m                     # 数学库
)

add_library(AudioRT_FFT_Process_lib SHARED src/utool_AudioRT/Audio_RT_FFT.cpp) # 添加动态库
target_link_libraries(AudioRT_FFT_Process_lib
    PkgConfig::FFTW3  # 单精度版本
)
add_library(AudioRT_Process_lib SHARED src/utool_AudioRT/Audio_RT_Process.cpp) # 添加动态库
add_library(SPE_Tradition_lib SHARED src/utool_AudioRT/SPE_Tradition.cpp) # 添加动态库
add_library(SPE_BeamForming_lib SHARED src/utool_AudioRT/SPE_BeamForming.cpp) # 添加动态库
add_library(AudioRT_lib SHARED src/utool_AudioRT/Audio_RT_Base.cpp) # 添加动态库
add_library(Globals_lib SHARED src/utool_AudioRT/Globals.cpp) # 添加动态库
add_library(HardwareThread_RT_lib SHARED src/utool_AudioRT/HardwareThread_RT.cpp) # 添加动态库

# 链接库
target_link_libraries(AudioRT_lib
    PkgConfig::PORTAUDIO  # PortAudio库
    AudioRT_Process_lib
    AudioRT_FFT_Process_lib
    SPE_Tradition_lib
    SPE_BeamForming_lib
    HardwareThread_RT_lib # 硬件控制库
    Globals_lib # 全局变量控制
    ${WIRINGPI_LIBRARY}   # wiringPi库
    Threads::Threads      # 线程库（pthread）
    DeepCMP_RT_lib # ASC的RKNN库
    
)
add_executable(main_AudioRT src/main_AudioRT.cpp)
# 链接库
target_link_libraries(main_AudioRT
    AudioRT_lib    # 你的动态库
)


# 音频校准代码(扬声器校准)
# 添加可执行文件
add_executable(Audio_Calibration Audio_Calibration.cpp)
# 链接库
target_link_libraries(Audio_Calibration
    AudioRT_lib    # 你的动态库
)


# //测试代码--ALSA代码功能测试
add_library(AudioRT_FFT_KF_Process_lib SHARED src/utool_AudioRT/Audio_RT_FFT_KF.cpp) # 添加动态库

add_library(AudioRT_KF_Process_lib SHARED src/utool_AudioRT/Audio_RT_Process_KF.cpp) # 添加动态库
add_library(AudioRT_KF_lib SHARED src/utool_AudioRT/Audio_RT_Base_KF.cpp) # 添加动态库


# 链接库
target_link_libraries(AudioRT_KF_lib
    PkgConfig::PORTAUDIO  # PortAudio库
    AudioRT_KF_Process_lib
    AudioRT_FFT_KF_Process_lib
    SPE_Tradition_lib
    SPE_BeamForming_lib
    HardwareThread_RT_lib # 硬件控制库
    Globals_lib # 全局变量控制
    ${WIRINGPI_LIBRARY}   # wiringPi库
    Threads::Threads      # 线程库（pthread）
    DeepCMP_RT_lib # ASC的RKNN库
)

# 添加可执行文件
add_executable(ALSA_Test src/ALSA_Test.cpp)
# 链接库
target_link_libraries(ALSA_Test
    AudioRT_KF_lib
)





# //测试代码--ASC的DeepCMP代码功能测试
# 构建自定义封装API库

# 添加可执行文件
add_library(Preprocess_CMP_lib SHARED src/AscDeepFile/Preprocess_CMP.cpp) # 添加动态库
add_library(DeepCMP_lib SHARED src/AscDeepFile/DeepCMP.cpp) # 添加动态库

target_link_libraries(DeepCMP_lib
    Preprocess_CMP_lib
    AudioRT_lib
    rknn_engine
    sndfile      # libsndfile的CMake目标
    ${KISSFFT_LIB}        # kissfft库
    m                     # 数学库
)
add_executable(DeepCMP_Test src/DeepCMP_Test.cpp)
# 链接库
target_link_libraries(DeepCMP_Test
    DeepCMP_lib
)


# //测试代码--ASC的DeepCMP_RT代码功能测试
# 构建自定义封装API库

# 添加可执行文件
add_executable(DeepCMP_RT_Test src/DeepCMP_RT_Test.cpp)
# 链接库
target_link_libraries(DeepCMP_RT_Test
    AudioRT_lib
    DeepCMP_RT_lib
)




# //测试代码
# 使用pkg-config查找libsndfile
pkg_check_modules(SNDFILE REQUIRED sndfile)

# 添加可执行文件
add_executable(main_AudioRT_Test Main_Audio_Test.cpp)

# 包含目录
include_directories(${SNDFILE_INCLUDE_DIRS})

# 链接库
target_link_libraries(main_AudioRT_Test 
    AudioRT_lib
    ${SNDFILE_LIBRARIES}
)